package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Api(tags = "地址相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;



    @GetMapping("/default")
    public R queryDefault(){
        // select * from address_book where is_default = 1 and user_id = ?
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        qw.eq(AddressBook::getUserId,userId);
        qw.eq(AddressBook::getIsDefault,1);
        AddressBook one = addressBookService.getOne(qw);
        return R.success(one);
    }

    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);

        return R.success("修改成功");
    }

    @DeleteMapping
    public R<String> deleteAddress(@RequestParam List<Long> ids){
        addressBookService.removeByIds(ids);
        return R.success("删除成功！！");
    }

    @GetMapping("/{id}")
    public R<AddressBook> queryById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }


    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R  defaultAddress(@RequestBody AddressBook addressBook){

        Long userId = BaseContext.getCurrentId();
        // 1、把当前用户的所有地址的 is_default 设置为 0
        // update address_book set is_default = 0 where user_id = ?
        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault,0);//set is_default = 0
        qw.eq(AddressBook::getUserId, userId);
        addressBookService.update(qw);

        //2、 设置当前地址的 is_default = 1
        //  update address_book set is_default = 1 where id = ?
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置成功！");
    }

    /**
     * 新增地址
     * @param addressBook
     * @param session
     * @return
     */
    @PostMapping
    public R saveAddress(
            @RequestBody AddressBook addressBook,
            HttpSession session){
        //需要设置地址对应的user_id信息
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);
        return R.success("保存地址成功！");
    }


    /**
     * 根据登录用户id查询地址列表
     * @param session
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> listByUserId(HttpSession session){
        // 根据用户id查询地址列表数据 select * from address_book where user_id = ?
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        //从session中获取用户id
        Long userId = (Long) session.getAttribute("user");
        qw.eq(AddressBook::getUserId,userId);
        List<AddressBook> list = addressBookService.list(qw);
        return R.success(list);
    }
}
